package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "帖子回复接口")
@RequestMapping("reply")
@Slf4j
public class ArticleReplyController {

    @Resource
    private IArticleReplyService articleReplyService;
    @Resource
    private IArticleService articleService;
    /**
     * 回复
     * @param request
     * @param articleId
     * @param content
     * @return
     */
    @PostMapping("/create")
    @ApiOperation("新增一条回复")
    public AppResult create(HttpServletRequest request
            ,@ApiParam(value = "帖⼦Id") @RequestParam(value = "articleId") @NonNull Long articleId
            , @ApiParam(value = "回复内容") @RequestParam(value = "content") @NonNull String content) {
        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if (user == null || user.getDeleteState() == 1 ) {
            return AppResult.failed(ResultCode.FAILED_USER_NOTEXISTS);
        }
        if ( user.getState() == 1) {
            return AppResult.failed("用户状态异常");
        }
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        if (article.getState() == 1) {
            return AppResult.failed("文章状态异常");
        }

        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReply.setPostUserId(user.getId());
        articleReplyService.create(articleReply);
        return AppResult.success("新增成功");
    }


    /**
     * 帖子回复展示
     * @return
     */
    @ApiOperation("回复帖⼦")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(
            @ApiParam(value = "帖⼦Id") @RequestParam(value = "articleId") @NonNull Long articleId) {
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        if (articleReplies == null) {
            articleReplies = new ArrayList<>();
        }
        return AppResult.success(articleReplies);
    }
}
